﻿# -*- coding: utf-8 -*-
from PIL import Image

codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'. '''
count = len(codeLib)

def transform(image_file):
    codePic = ''
    for h in range(0,image_file.size[1]):
        for w in range(0,image_file.size[0]):
            g,r,b = image_file.getpixel((w,h))
            gray = int(r* 0.299+g* 0.587+b* 0.114)
            codePic = codePic + codeLib[int(((count-1)*gray)/256)]
        codePic = codePic+'\r\n'
    return codePic

fname=input('输入文件名:\n')+'.jpg'
fp = open(fname,'rb')

image_file = Image.open(fp)
bl=int(input('比例大小:\n'))
kd=int(image_file.size[0]*0.075*bl)
cd=int(image_file.size[1]*0.05*bl)
if(kd>1024):
    cd=int(1024*cd/kd)
    kd=1024
    print('比例过大，已自动调整')
image_file=image_file.resize((kd, cd))#调整图片大小
print (u'Info:',image_file.size[0],' ',image_file.size[1],' ',count)

tmp = open('tmp.txt','w')
tmp.write(transform(image_file))
tmp.close()



'''
def transform1(image_file):
    image_file = image_file.convert("L")
    codePic = ''
    for h in range(0,image_file.size[1]):  
        for w in range(0,image_file.size[0]):
            gray = image_file.getpixel((w,h)) 
            codePic = codePic + codeLib[int(((count-1)*gray)/256)]
        codePic = codePic+'\r\n'
    return codePic
'''